package util.beans;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;

import util.clock.VectorClock;
import util.interfaces.IObjectToReceive;

public class ObjectToReceive implements IObjectToReceive{

	@Override
	public Object getPayload(Object recPacket) throws IOException, ClassNotFoundException {
		if (recPacket instanceof DatagramPacket) {
			
			ByteArrayInputStream input = new ByteArrayInputStream(
					((DatagramPacket)recPacket).getData(), 
					((DatagramPacket)recPacket).getOffset(),
					((DatagramPacket)recPacket).getLength());
			
			ObjectInputStream oIn = new ObjectInputStream(input);
			
			Object object2Return = oIn.readObject();
			VectorClock.getInstance().receive((Integer[]) oIn.readObject());
			
			return object2Return;
		}
		
		return null;
	}
	
}
